package cn.sxgan.core.services.impl;

import cn.sxgan.common.entity.SysMenu;
import cn.sxgan.common.entity.converts.ISysMenuConvert;
import cn.sxgan.common.entity.query.SysMenuQuery;
import cn.sxgan.common.entity.vo.SysMenuVO;
import cn.sxgan.common.enums.KeyPrefixEnum;
import cn.sxgan.common.enums.ResStatusEnum;
import cn.sxgan.common.exception.ApiException;
import cn.sxgan.common.redis.RedisUtil;
import cn.sxgan.common.response.Result;
import cn.sxgan.common.utils.CommonUtils;
import cn.sxgan.core.services.IMenuService;
import cn.sxgan.db.mappers.ISysMenuMapper;
import com.github.pagehelper.PageHelper;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Description: 菜单服务
 * @Author: sxgan
 * @Date: 2025-01-03 09:32
 * @Version: 1.0
 **/
@Slf4j
@Service
public class MenuServiceImpl implements IMenuService {
    
    @Resource
    private ISysMenuMapper sysMenuMapper;
    @Autowired
    private RedisUtil redisUtil;
    
    @Override
    public Result<List<SysMenuVO>> findMenuList(SysMenuQuery sysMenuQuery) {
        try {
            CommonUtils.initPageParam(sysMenuQuery);
            PageHelper.startPage(sysMenuQuery.getPage(), sysMenuQuery.getSize());
            List<SysMenu> sysMenus = sysMenuMapper.selectSysMenuByCondition(sysMenuQuery);
            List<SysMenuVO> sysMenuVOS = ISysMenuConvert.INSTANCE.convertList(sysMenus);
            return Result.success(sysMenuVOS);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("异常", e);
            throw new ApiException(ResStatusEnum.EXCEPTION_STATUS_999);
        }
    }
    
    @Override
    @Transactional
    public Result<String> addMenu(SysMenuVO sysMenuVO) {
        try {
            SysMenu sysMenu = ISysMenuConvert.INSTANCE.convert(sysMenuVO);
            if (sysMenu.getMenuId() == null) {
                String menuId = redisUtil.nextId(KeyPrefixEnum.MENU_ID_PRE, 10, 1, true);
                assert menuId != null;
                sysMenu.setMenuId(Long.parseLong(menuId));
            }
            int flag = sysMenuMapper.insert(sysMenu);
            if (flag > 0) {
                return Result.success("新增成功");
            }
            return Result.fail("新增失败");
        } catch (Exception e) {
            e.printStackTrace();
            log.error("异常", e);
            throw new ApiException(ResStatusEnum.EXCEPTION_STATUS_725.getMsg());
        }
    }
    
    @Override
    @Transactional
    public Result<String> bindRoleAndMenu(Long roleId, List<Long> menuIdList) {
        if (roleId == null || menuIdList == null || menuIdList.isEmpty()) {
            throw new ApiException(ResStatusEnum.EXCEPTION_STATUS_900);
        }
        try {
            // 移除已绑定的菜单
            List<Long> menuIds = sysMenuMapper.selectMenuIdByRoleId(roleId);
            menuIdList.removeAll(menuIds);
            // 新增未绑定的菜单
            if (menuIdList.isEmpty()) {
                return Result.fail(ResStatusEnum.EXCEPTION_STATUS_727.getMsg());
            } else {
                int i = sysMenuMapper.insertRoleAndMend(roleId, menuIdList);
                if (i > 0) {
                    return Result.success("绑定成功");
                }
            }
            return Result.fail("绑定失败");
        } catch (Exception e) {
            e.printStackTrace();
            log.error("异常", e);
            throw new ApiException(e.getMessage());
        }
    }
    
    @Override
    public Result<String> editMenu(SysMenuVO sysMenuVO) {
        if (sysMenuVO == null || sysMenuVO.getMenuId() == null) {
            throw new ApiException(ResStatusEnum.EXCEPTION_STATUS_900.getMsg());
        }
        try {
            SysMenu oldSysMenu = sysMenuMapper.selectById(sysMenuVO.getMenuId());
            if (oldSysMenu == null) {
                return Result.fail(ResStatusEnum.EXCEPTION_STATUS_728.getMsg());
            }
            SysMenu sysMenu = ISysMenuConvert.INSTANCE.convert(sysMenuVO);
            int update = sysMenuMapper.updateById(sysMenu);
            if (update > 0) {
                return Result.success(ResStatusEnum.UPDATE_STATUS_200.getMsg());
            }
            return Result.fail(ResStatusEnum.UPDATE_STATUS_500.getMsg());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("异常", e);
            throw new ApiException(ResStatusEnum.EXCEPTION_STATUS_999);
        }
    }
    
    @Override
    public Result<String> removeMenu(List<Long> menuIds) {
        if (menuIds == null || menuIds.size() <= 0) {
            throw new ApiException(ResStatusEnum.EXCEPTION_STATUS_900.getMsg());
        }
        try {
            int i = sysMenuMapper.deleteBatchIds(menuIds);
            if (i > 0) {
                return Result.success(ResStatusEnum.DELETE_STATUS_200.getMsg());
            }
            return Result.fail(ResStatusEnum.DELETE_STATUS_500.getMsg());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("异常", e);
            throw new ApiException(e.getMessage());
        }
        
    }
}
